package com.offcn.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.offcn.entity.Course;
import com.offcn.service.CourseByService;
import com.offcn.service.CourseByServiceImpl;
import com.offcn.utlis.BaseServlet;
import com.offcn.utlis.FileUtlis;
import com.offcn.utlis.PageUtils;
import com.offcn.utlis.ResultVo;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@WebServlet("/course")
@MultipartConfig  //图片处理
public class CourseServlet extends BaseServlet {
    ResultVo resultVo = null;
    ObjectMapper objectMapper = new ObjectMapper();
      CourseByService courseByService = new CourseByServiceImpl();
    // 图片 与视频的 保存以及回显
    public  void uploadFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String fileUtlis = "";
        try {
             fileUtlis =FileUtlis.FileSet(req);
            if (fileUtlis.contains(".jpg") || fileUtlis.contains(".jpeg") || fileUtlis.contains(".png") || fileUtlis.contains(".gif") || fileUtlis.contains("bmp")){
                resultVo = new ResultVo(201,"图片上传成功",fileUtlis);
            }else {
                resultVo = new ResultVo(202,"视频上传成功",fileUtlis);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultVo = new ResultVo(500,"上传失败",null);
        } finally {
            resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
        }
    }

    // 图片的删除
    public void delUpload(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String peasm = req.getParameter("peasm");
        File file = new File("C:\\Users\\Administrator\\Desktop\\picture",peasm);
        boolean delete = file.delete();
        System.out.println(delete);
    }

    //添加课程
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Course course = new Course();
        BeanUtils.populate(course,req.getParameterMap());
           boolean b  = courseByService.add(course);
           if (b){
               resultVo = new ResultVo(200,"添加课程成功",null);
           }else {
               resultVo = new ResultVo(500,"添加课程失败",null);
           }
           resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
    }

    // 搜索
    public void find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int currentPage =Integer.parseInt(req.getParameter("currentPage"))  ; // 当前页
        int pageSize = Integer.parseInt(req.getParameter("pageSize")) ; // 每页显示页量
        String search = req.getParameter("search");  // 搜索

       PageUtils<Course> pageUtils = courseByService.find(currentPage,pageSize,search);
        resultVo = new ResultVo(200,"查询成功",pageUtils);
        resp.getWriter().println(objectMapper.writeValueAsString(resultVo));

    }

    // 修改
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Course course = new Course();
        BeanUtils.populate(course,req.getParameterMap());
        boolean b  = courseByService.updateCourse(course);
        if (b){
            resultVo = new ResultVo(200,"修改课程成功",null);
        }else {
            resultVo = new ResultVo(500,"修改课程失败",null);
        }
        resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
    }

    // 删除
    public  void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收 客户端发来的请求数据
        String[] cids = req.getParameterValues("cid");
            // 去往业务层查询
              boolean k = courseByService.delete(cids);
              // 返回的结果进行判断
              if (k){
                  resultVo = new ResultVo(200,"删除成功",null);
              }else {
                  resultVo = new ResultVo(500,"删除失败",null);
              }

              resp.getWriter().print(objectMapper.writeValueAsString(resultVo));
    }

    //查询全部课程
    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

       List<Course> courseList = courseByService.findAll();

        resultVo = new ResultVo(200,"查询成功",courseList);

        resp.getWriter().println(objectMapper.writeValueAsString(resultVo));
    }
}
